import Konva from 'konva'

const contourGroup = new Konva.Group()

const contourPoint = [
  [90, 80, 600, 80], // 1
  [600, 80, 600, 350], // 2
  [600, 350, 1400, 350], // 3
  [1400, 350, 1400, 680], // 4
  [1400, 680, 1800, 680], // 5
  [1800, 680, 1800, 940], // 6
  [1800, 940, 1100, 940], // 7
  [1100, 940, 1100, 680], // 8
  [1100, 680, 1300, 680], // 9
  [1300, 680, 1300, 640], // 10
  [1300, 640, 1100, 640], // 11
  [1100, 640, 1100, 490], // 12
  [1100, 490, 1300, 490], // 13
  [1300, 490, 1300, 450], // 14
  [1300, 450, 90, 450], // 15
  [90, 450, 90, 80], // 16
]

// 绘制地图轮廓
contourPoint.map((item) => {
  const line = new Konva.Line({ stroke: '#68c6ff', strokeWidth: 10, points: item, lineCap: 'round' })
  contourGroup.add(line)
})

const barcodeGroup = new Konva.Group()

export const commonPoint = {
  data: [
    { id: 1, x: 240, y: 300 },
    { id: 2, x: 240, y: 400 },
    { id: 3, x: 450, y: 400 },
    { id: 4, x: 450, y: 300 },
    { id: 5, x: 600, y: 400 },
    { id: 6, x: 720, y: 400 },
    { id: 7, x: 810, y: 400 },
    { id: 8, x: 900, y: 400 },
    { id: 9, x: 990, y: 400 },
    { id: 10, x: 1080, y: 400 },
    { id: 11, x: 1170, y: 400 },
    { id: 12, x: 1260, y: 400 },
    { id: 13, x: 1350, y: 400 },
    { id: 14, x: 1350, y: 470 },
    { id: 15, x: 1350, y: 540 },
    { id: 16, x: 1160, y: 540 },
    { id: 17, x: 1350, y: 590 },
    { id: 18, x: 1220, y: 590 },
    { id: 19, x: 1350, y: 730 },
    { id: 20, x: 1550, y: 730 },
    { id: 21, x: 1550, y: 780 },
  ],
  link: ['1-2', '3-4', '2-13', '15-16', '13-19', '17-18', '19-20', '20-21'],
}

export const getPointXY = (id, agvID) => {
  const findPoint = (val) => commonPoint.data.find((item) => item.id === val)

  if ([61, 62].includes(id)) return findPoint(1)
  if ([53, 54, 55, 56, 57, 58, 59, 60].includes(id)) return findPoint(2)
  if ([103, 44, 45, 46, 47, 48, 49, 50].includes(id)) return findPoint(3)
  if ([51, 52].includes(id)) return findPoint(4)
  if ([33, 34, 35, 36, 37, 38, 39, 40, 41, 42].includes(id)) return findPoint(5)
  if ([31, 32].includes(id)) return findPoint(6)
  if ([29, 30].includes(id)) return findPoint(7)
  if ([27, 28].includes(id)) return findPoint(8)
  if ([25, 26].includes(id)) return findPoint(9)
  if ([23, 24].includes(id)) return findPoint(10)
  if ([21, 22].includes(id)) return findPoint(11)
  if ([19, 20].includes(id)) return findPoint(12)
  if ([15, 101, 17, 18].includes(id)) return findPoint(13)
  if ([13, 14].includes(id)) return findPoint(14)
  if ([3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 85, 86, 87, 88, 89, 93, 94, 107, 108].includes(id)) {
    if (agvID === 1) return findPoint(15)
    if (agvID === 2) return findPoint(17)
  }
  if ([0].includes(id)) {
    if (agvID === 1) return findPoint(16)
    if (agvID === 2) return findPoint(18)
  }
  if ([1, 2].includes(id)) return findPoint(16)
  if ([90, 91].includes(id)) return findPoint(18)
  if ([63, 64, 65, 66, 67, 68, 69, 70, 71, 95].includes(id)) return findPoint(19)
  if ([72, 75, 76, 77, 78, 80, 81, 82].includes(id)) return findPoint(20)
  if ([83, 84].includes(id)) return findPoint(21)
}

// 绘制色带
commonPoint.link.forEach((item) => {
  const { x: x1, y: y1 } = commonPoint.data.find(({ id }) => id === +item.split('-')[0])
  const { x: x2, y: y2 } = commonPoint.data.find(({ id }) => id === +item.split('-')[1])
  const line = new Konva.Line({
    stroke: '#f90',
    strokeWidth: 2,
    points: [x1, y1, x2, y2],
    tension: 1,
  })
  barcodeGroup.add(line)
})

// 绘制地图点
commonPoint.data.forEach((item) => {
  const { x, y } = item
  const node = new Konva.Group({ id: String(item.id), x, y })
  const rect = new Konva.Rect({
    cornerRadius: 0.5,
    fill: '#e33',
    height: 10,
    offset: { x: 5, y: 5 },
    width: 10,
  })
  const path = new Konva.Path({
    data: 'M0 0h78.77v1024H0z M157.54 0v1024h157.54V0zM393.85 1024h78.77V0H393.85zM551.38 0v1024h157.54V0zM787.69 0v1024h157.54V0zM1102.77 0h-78.77v1024h157.54V0z',
    fill: 'black',
    offset: { x: 600, y: 500 },
    scale: { x: 0.006, y: 0.006 },
  })
  barcodeGroup.add(node.add(rect).add(path))
})

const markGroup = new Konva.Group()

const markPoint = [
  { id: '取料升降台', x: 240, y: 250 },
  { id: '放料升降台', x: 1550, y: 830 },
  { id: '清洗架', x: 450, y: 250 },
]

markPoint.map((item) => {
  const node = new Konva.Group(item)
  const mark = new Konva.Text({
    fontSize: 24,
    align: 'center',
    text: item.id,
    fill: '#fff',
  })
  mark.offset({ x: mark.width() / 2, y: mark.height() / 2 })
  const rect = new Konva.Rect({
    cornerRadius: 5,
    fill: '#e33',
    height: mark.height() + 20,
    width: mark.width() + 20,
  })
  rect.offset({ x: rect.width() / 2, y: rect.height() / 2 + 2 })
  markGroup.add(node.add(rect).add(mark))
})

export const mapInfoGroup = new Konva.Group()

mapInfoGroup.add(contourGroup).add(barcodeGroup).add(markGroup)
